package cn.ulyer.core.modules.system.rest;


import cn.hutool.core.lang.Assert;
import cn.ulyer.common.annotation.Log;
import cn.ulyer.common.constants.ResultCode;
import cn.ulyer.common.constants.StatusEnum;
import cn.ulyer.common.jpa.entity.Dict;
import cn.ulyer.common.jpa.entity.DictItem;
import cn.ulyer.common.jpa.entity.QDictItem;
import cn.ulyer.common.model.PageableVo;
import cn.ulyer.common.utils.PageResult;
import cn.ulyer.common.utils.Result;
import cn.ulyer.core.modules.system.service.DictItemService;
import cn.ulyer.core.modules.system.service.DictService;
import com.querydsl.core.types.Predicate;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictRest")
public class DictRest {


    private static final String MODULE = "dict";
    @Autowired
    private DictService dictService;

    @Autowired
    private DictItemService dictItemService;

    /**
     * 分页
     */
    @GetMapping("/page")
    public Result<PageResult<Dict>> page(PageableVo pageableVo, Dict dict){
        PageResult result = dictService.page(pageableVo.render(),dict);
        return Result.ok().data(result);
    }


    @PostMapping("/create")
    @Log(module = MODULE,action = Log.ActionName.CREATE)
    public Result<Integer> create(@RequestBody Dict dict){
        dictService.save(dict);
        return Result.ok(ResultCode.CREATE).data(dict.getDictId());
    }


    @PutMapping("/update")
    @Log(module = MODULE,action = Log.ActionName.UPDATE)
    public Result update(@RequestBody Dict dict){
        dictService.updateById(dict);
        return Result.ok(ResultCode.UPDATE);
    }

    /**
     * removeById
     *
     */
    @DeleteMapping("/remove")
    @Log(module = MODULE,action = Log.ActionName.DELETE)
    public Result remove(@RequestParam Integer id){
        dictService.deleteById(id);
        return Result.ok(ResultCode.DELETE);
    }

    @GetMapping("/details")
    public Result<List<DictItem>> detail(@RequestParam Integer dictId){
        QDictItem dictItem = QDictItem.dictItem;
        return Result.ok().data(dictItemService.findAll(dictItem.dictId.eq(dictId)));
    }

    @PostMapping("/createItem")
    @Log(module = MODULE,action = "添加字典细项")
    public Result<Integer> createItem(@RequestBody DictItem dictItem){
        Assert.notNull(dictItem.getDictId());
        QDictItem qDictItem = QDictItem.dictItem;
        long c = dictItemService.count(qDictItem.dictId.eq(dictItem.getDictId()).and(qDictItem.dictKey.eq(dictItem.getDictKey()))
        .and(qDictItem.status.eq(StatusEnum.ENABLE.status())));
        if(c>0L){
            return Result.fail("已存在相同字典明细");
        }
        dictItemService.save(dictItem);
        return Result.ok().data(dictItem);
    }

    @DeleteMapping("/removeItem")
    @Log(module = MODULE,action = "删除字典细项")
    public Result removeItem(@RequestParam Integer itemId){
        dictItemService.deleteById(itemId);
        return Result.ok();
    }


}
